//
//  LeftViewController.m
//  QuickCare
//
//  Created by lidabiao on 16/7/11.
//  Copyright © 2016年 lidabiao. All rights reserved.
//

#import "LeftViewController.h"
#import "loginPhoneViewController.h"
#import "DrawerManager.h"
#import "headView.h"
#import "CoreDataManager.h"
#import "PushTableViewController.h"
#import "SettingViewController.h"
#import "registerNewAccountController.h"
#import "EditViewController.h"

@interface LeftViewController () <clickLoginButtonDelegate>

@property (nonatomic, weak) headView *header;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@", NSHomeDirectory());
    headView *headV = [headView headView];
    
    self.tableView.tableHeaderView = headV;
    
    self.header = headV;
    
    headV.loginDelegate = self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.userInfo = [self loadLastLoginUser];
}

-(void)setUserInfo:(UserInfo *)userInfo
{
    _userInfo = userInfo;
    
    self.header.userInfo = userInfo;
    
    [self.tableView reloadData];
}

///  调取工具数据库中最后登录的用户信息
-(UserInfo *)loadLastLoginUser
{
    NSArray *arr = [kCoreDataManager executeFetchWithEntityName:@"UserInfo" format:@"isLogin == YES"];
    
    if (arr)
    {
        return arr.lastObject;
    }
    return nil;
}

#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.userInfo.isLogin ? 2 : 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.userInfo.isLogin ? 3 : 0;
}

#pragma mark - headView代理
-(void)clickLoginButtonWithView:(UIView *)headView
{
        UIStoryboard *st = [UIStoryboard storyboardWithName:@"loginPhone" bundle:nil];
    
        loginPhoneViewController *loginVC = [st instantiateInitialViewController];
    
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
    
    [kDrawerManager presentViewController: nav];
    
}

-(void)clickRegisterButtonWithView:(UIView *)headview
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"registerNewAccountController" bundle:nil];
    
    registerNewAccountController *newVc = [sb instantiateInitialViewController];
    
    UINavigationController *registVC = [[UINavigationController alloc] initWithRootViewController:newVc];
    
    newVc.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemClick)];
    
    [kDrawerManager presentViewController:registVC];
}

-(void)tapGestureAction
{
    UIStoryboard *st = [UIStoryboard storyboardWithName:@"Edit" bundle:nil];
    
    EditViewController *editVC = [st instantiateInitialViewController];
    
    editVC.userInfo = self.userInfo;
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:editVC];
    
    [kDrawerManager presentViewController:nav];
}

#pragma mark - 返回
-(void)backBarButtonItemClick
{
    [kDrawerManager enableLeftDrawerAndOpenLeftDrawer];
}

#pragma mark - 点击cell方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
    
        if (indexPath.row == 1) {
        
            UINavigationController *masterVC = [[UIStoryboard storyboardWithName:@"bingliMaster" bundle:nil] instantiateInitialViewController];
            
            [kDrawerManager presentViewController:masterVC];
        
        }
    
    }
    
    if (indexPath.section == 1 ) {
        
        if (indexPath.row == 1){
            
            UINavigationController *aboutVC = [[UIStoryboard storyboardWithName:@"About" bundle:nil] instantiateInitialViewController];
            
            
            [kDrawerManager presentViewController:aboutVC];
            
        }
        
        if (indexPath.row == 0){
            
            SettingViewController *settingVC = [[SettingViewController alloc] init];
            
            settingVC.userInfo = self.userInfo;
            
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:settingVC];
            
            [kDrawerManager presentViewController:nav];
            
        }
        
    }

}




@end
